/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote.gui;

import java.io.InputStream;

import org.eclipse.swt.widgets.Composite;

import pl.vdl.azbest.annotations.*;
import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.log.gui.composite.CompositeSystemConsole;
import pl.vdl.azbest.mremote.CommComunicator;
import pl.vdl.azbest.mremote.com.CConnect;
import pl.vdl.azbest.mremote.com.CommObserver;
import pl.vdl.azbest.mremote.com.CommState;

import pl.vdl.azbest.mremote.com.TextOutputStream;
import pl.vdl.azbest.mremote.gui.composite.CompositeCellSignalTable;
import pl.vdl.azbest.mremote.gui.composite.CompositeComm;
import pl.vdl.azbest.mremote.gui.composite.CompositeCommSend;
import pl.vdl.azbest.mremote.gui.composite.CompositeConsole;
import pl.vdl.azbest.mremote.probe.signal.CellSignalData;
import pl.vdl.azbest.mremote.probe.signal.CellSignalProbe;
import pl.vdl.azbest.mremote.probe.signal.CellSignalProbeObserver;
import static pl.vdl.azbest.mremote.com.CommState.*;

@TODO(dateAssigned = "26-03-2008", target = "Add new elements as application grows", done = "", responsible = Team.AZBEST

)
/**
 * This class is interface (facade) fro ALl gui. Application engine talks to GUI
 * using its methods. It is Observer of many engine elements and provides
 * register of active SWT COmposite objects. <br>
 * </br>
 * <ol>
 * <li>Add new SWT Composite,
 * <li>Create ist API for facade and add add to GUIFacade
 * <li>Add API to Dummy object </ol?
 * 
 * 
 */
public class GUIFacade implements CommObserver, CellSignalProbeObserver {
	{
		LoggingTheGathering.addPath(getClass().getName());
	}

	private static GUIFacade instance;

	private CompositeComm cComm;

	private CompositeConsole cConsole;

	private CompositeSystemConsole cSystemConsole;

	private CompositeCommSend cCommSend;

	private CompositeCellSignalTable ccsTable;

	private GUIFacade() {
		CConnect.getInstance().registerObserver(this);
		CellSignalProbe.getInstance().addObservers(this);
	}

	public static GUIFacade getInstance() {
		if (instance != null) {
			return instance;
		} else {
			synchronized (GUIFacade.class) {
				instance = new GUIFacade();
				return instance;
			}
		}
	}

	public void update(final CommState state) {
		// update to gui associated with Comm communicaton
		if (this.cConsole != null) {
			cConsole.getDisplay().asyncExec(new Runnable() {
				public void run() {
					cConsole.toConsole(state.getGUITxt());
				}
			});

		}
	}

	public void addComposite(Composite composite) {
		if (composite instanceof CompositeComm) {
			cComm = (CompositeComm) composite;
		} else if (composite instanceof CompositeConsole) {

			cConsole = (CompositeConsole) composite;
			textOutputStream = cConsole.getTextOutputStream();
		} else if (composite instanceof CompositeSystemConsole) {
			cSystemConsole = (CompositeSystemConsole) composite;
		} else if (composite instanceof CompositeCommSend) {
			cCommSend = (CompositeCommSend) composite;
			textInputStream = cCommSend.getTextInputStream();
		}else if(composite instanceof CompositeCellSignalTable){
			ccsTable = (CompositeCellSignalTable)composite;
			
		}
	}

	public void removeComposite(Composite composite) {
		if (composite instanceof CompositeComm) {
			cComm = null;
		} else if (composite instanceof CompositeConsole) {
			cConsole = null;
		} else if (composite instanceof CompositeSystemConsole) {
			cSystemConsole = null;
		} else if (composite instanceof CompositeCommSend) {
			cCommSend = null;
		}
	}

	private TextOutputStream textOutputStream;

	public TextOutputStream getTextOutputStream() {
		return textOutputStream;
	}

	private InputStream textInputStream;

	public InputStream getTextInputStream() {
		return textInputStream;
	}

	public void update(CellSignalData csd) {
		ccsTable.loadCellSignalData(csd);
	}

}
